package com.cwvs.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cwvs.services.AppOrderService;
import com.cwvs.services.AppPayService;
import com.cwvs.utils.JsonUtil;
import com.cwvs.utils.Pages;

@Controller
@RequestMapping("/admin")
public class AdminAppPayController {
	@Resource
	private AppPayService appPayService;
	@Resource
	private AppOrderService appOrderService;

	@RequestMapping("/pay_view")
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_pay/app_pay_list");
		return mv;
	}

	@RequestMapping("/pay_list")
	public @ResponseBody
	Map<String, Object> payList(HttpServletRequest request,
			HttpServletResponse response) {
		Pages page = new Pages();
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		page.setPageSize(Integer.parseInt(limit));
		page.setCurrentPage(Integer.parseInt(start) / Integer.parseInt(limit)
				+ 1);
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		Pages rePage = appPayService.payList(paramsMap, page);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", rePage.getPage());
		ret.put("totalCount", rePage.getTotalRecord());
		return ret;
	}

	@RequestMapping("/pay_edit")
	public ModelAndView storeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("app_pay/app_pay_edit");
		mv.addObject("types",
				JsonUtil.toJson(appOrderService.getAllOrderType()));
		mv.addObject("pays", JsonUtil.toJson(appPayService.getAllPay()));
		return mv;
	}

	@RequestMapping("/pay_save")
	public @ResponseBody
	Map<String, Object> paySave(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> res = new HashMap<String, Object>();
		String orderType = request.getParameter("order_type");
		String payId = request.getParameter("pay_id");
		if (appPayService.isExists(orderType, payId)) {
			res.put("errormsg", "数据已存在");
			res.put("success", false);
			return res;
		} else {
			boolean b = appPayService.savePay(orderType, payId);
			if (b) {
				res.put("errormsg", "保存成功");
				res.put("success", true);
				return res;
			} else {
				res.put("errormsg", "保存失败");
				res.put("success", false);
				return res;
			}

		}

	}

	@RequestMapping("/pay_tf")
	public @ResponseBody
	Map<String, Object> payTf(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> res = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String isActive = request.getParameter("isActive");
		boolean status=StringUtils.equalsIgnoreCase(isActive, "1");
		if(appPayService.updateStatus(id, status)){
			res.put("errormsg", "更新成功");
			res.put("success", true);
			return res;
		}
		res.put("errormsg", "数据已存在");
		res.put("success", false);
		return res;

	}
}
